package models

var classStore ClassStorer

type ClassError struct {
	errorText string
}

func NewClassError(errorMessage string) error {
	return ClassError{errorMessage}
}

func (c ClassError) Error() string {
	return "ClassError: " + c.errorText
}

func panicIfUnsetClassStorer() {
	if classStore == nil {
		panic("Cannot perform operation on class instance with no classStorer set.")
	}
}

type Class struct {
	Id            int64
	Year          int64
	Institution   string
	PhotoLocation string
	PhotoWidth    int64
	PhotoHeight   int64
}

func NewClass(year int64, institution string, photoLocation string, photoWidth int64, photoHeight int64) *Class {
	return &Class{
		Year:          year,
		Institution:   institution,
		PhotoLocation: photoLocation,
		PhotoWidth:    photoWidth,
		PhotoHeight:   photoHeight}
}

type ClassStorer interface {
	Save(*Class) error //Update class with id if saved for first time.
	GetClassByHumanId(int64) (*Class, error)
	GetClassByYearAndInstitution(int64, string) (*Class, error)
	GetAllClasses() ([]Class, error)
}

func (p *Class) Save() error {
	return classStore.Save(p)
}

func GetClassByHumanId(humanId int64) (*Class, error) {
	return classStore.GetClassByHumanId(humanId)
}

func GetClassByYearAndInstitution(year int64, institution string) (*Class, error) {
	return classStore.GetClassByYearAndInstitution(year, institution)
}

func GetAllClasses() ([]Class, error) {
	return classStore.GetAllClasses()
}

func SetClassStorer(c ClassStorer) {
	classStore = c
}
